//
//  MSQueryRelationsController.m
//  觅尚
//
//  Created by No.8 on 2019/4/17.
//  Copyright © 2019 No.8. All rights reserved.
//

#import "MSReservationsListController.h"
#import "MSReservationsCell.h"
#import "MSQueryRelationModel.h"
#define pageSize 10

@interface MSReservationsListController ()<UITableViewDelegate,UITableViewDataSource,MSReservationsCellDelegate>
//通用导航栏
{
    CustomNavigationBar *naviagtionBar;
    
    NSInteger page;
    
    NSInteger count;
}
/** 表视图*/
@property(nonatomic,strong)UITableView *tableView;


/** 表视图*/
@property(nonatomic,strong)UIView *HeaderView;


/** 取反*/
@property(nonatomic,strong)UIButton *typeButton;

/** 遮盖层*/
@property(nonatomic,strong)UIView *backView;


/** 弹出View*/
@property(nonatomic,strong)UIView *showView;

/** 数组*/
@property(nonatomic,strong)NSMutableArray *RoomManagementArray;

/** uid*/
@property(nonatomic,strong)NSString *uidStr;

/** 昵称*/
@property(nonatomic,strong)NSString *NameStr;

/** 头像*/
@property(nonatomic,strong)NSString *PhotoStr;

/** UITextField * seachField*/
@property(nonatomic,strong)UITextField * seachField;



@end

@implementation MSReservationsListController

/** Nav实现 */
- (int)navBarBottom {
    if ([WRNavigationBar isIphoneX]) {
        return 88;
    } else {
        return 64;
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    self.view.backgroundColor = backgroundcolor;
    self.RoomManagementArray = [NSMutableArray new];
    
    Prepare *header = [Prepare headerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiHeaderRefresh)];
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    // 马上进入刷新状态
    self.tableView.mj_header = header;
    [self.tableView.mj_header beginRefreshing];
}
- (void)MJChiBaoZiHeaderRefresh{
    page = 1;
    [self ReloadData];
}
- (void)MJChiBaoZiFootderRefresh{
    if (count / pageSize < page) {
        [MBManager showBriefAlert:@"没有更多数据了"];
    }else{
        page++;
    }
    [self FootderRefreshReloadData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self CreateNavigationBar];
    
    [self CreatView];
    
    [self tableView];
    
    
    PrepareFooter *footder = [PrepareFooter footerWithRefreshingTarget:self refreshingAction:@selector(MJChiBaoZiFootderRefresh)];
    // 马上进入刷新状态
    self.tableView.mj_footer = footder;
}
- (void)CreatView{
    
    
    UIView * HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, [self navBarBottom], ScreenWidth, 150 * px)];
    HeaderView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:HeaderView];
    
    UITextField * seachField = [[UITextField alloc] initWithFrame:CGRectMake(50 * px, 15 * px, HeaderView.width - 100 * px, 120 * px)];
    seachField.placeholder = @"搜索订房人姓名";
    //    seachField.layer.borderColor = [UIColor grayColor].CGColor;
    //    seachField.layer.borderWidth = 1.0f;
    seachField.backgroundColor = BackViewColor;
    seachField.layer.cornerRadius = 10 * px;
    seachField.keyboardType = UIKeyboardTypeNumberPad;
    [seachField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
    [seachField setValue:[UIFont systemFontOfSize:SYRealValue(35 / 2)] forKeyPath:@"_placeholderLabel.font"];
    seachField.textColor = [UIColor whiteColor];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"搜索"]];
    imgView.frame = CGRectMake(20 * px, 30 * px, 60 * px, 60 * px);
    seachField.leftView = imgView;
    seachField.leftViewMode = UITextFieldViewModeAlways;
    [HeaderView addSubview:seachField];
    self.seachField = seachField;
}


- (void)ReloadData{
    [MBManager showLoading];
    [self.RoomManagementArray removeAllObjects];
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"type":@1,@"page":@(page),@"pageSize":@(pageSize),@"text":self.seachField.text};
    
    [HTTPRequest POST:MSgetAllPrincess params:params succeed:^(id responseObject) {
        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            count = [responseObject[@"data"][@"count"] integerValue];
            NSMutableArray *temporaryArray = [MSQueryRelationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userList"]];
            [temporaryArray addObjectsFromArray:self.RoomManagementArray];
            self.RoomManagementArray = temporaryArray;

//            self.RoomManagementArray = [MSQueryRelationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userList"]];
        }else{
            [self.tableView.mj_header endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page = [params[@"page"] integerValue];

        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}

- (void)FootderRefreshReloadData{
    
    [MBManager showLoading];
    [self.RoomManagementArray removeAllObjects];
    NSDictionary * params = @{@"busId":[WYCAccountTool unarchivebelongBusid],@"type":@1,@"page":@(page),@"pageSize":@(pageSize),@"text":self.seachField.text};
    
    [HTTPRequest POST:MSgetAllPrincess params:params succeed:^(id responseObject) {
        WYCLog(@"---------%@",responseObject);
        if (CodeStr) {
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            self.RoomManagementArray = [MSQueryRelationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userList"]];
            
            //            self.RoomManagementArray = [MSQueryRelationModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"userList"]];
        }else{
            [self.tableView.mj_footer endRefreshing];
            [MBManager hideAlert];
            [MBManager showBriefAlert:responseObject[@"msg"]];
            page --;
            
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.mj_footer endRefreshing];
        [MBManager hideAlert];
        [MBManager showBriefAlert:Failed];
    }];
}
/** 表视图*/
- (UITableView *)tableView{
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (!_tableView) {
        
        if (@available(iOS 11.0, *)) {
            
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            
        } else {
            
            self.automaticallyAdjustsScrollViewInsets = NO;
            
        }
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, [self navBarBottom] + 150 * px, ScreenWidth, ScreenHeight - [self navBarBottom] - 150 * px) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    return _tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.RoomManagementArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 180 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    MSReservationsCell * cell = [MSReservationsCell cellWithTableView:tableView];
    tableView.backgroundColor = [UIColor clearColor];
    //无色
    cell.AddButtonDelegate = self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.QueryRelationModel = self.RoomManagementArray[indexPath.row];
    [cell.AddButton addTarget:self action:@selector(AddButtonClick) forControlEvents:UIControlEventTouchUpInside];
    //    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

- (void)ClickAddButtonUid:(NSString *)UIDStr PhotoStr:(NSString *)PhotoStr NameStr:(NSString *)NameStr{
    //     WYCLog(@"=============%@",UID);
    self.uidStr = UIDStr;
    self.PhotoStr = PhotoStr;
    self.NameStr = NameStr;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    MSFinancialAdministrationController * AdministrationVC = [[MSFinancialAdministrationController alloc] init];
    //    [self.navigationController pushViewController:AdministrationVC animated:NO];
    
}
- (void)CreateNavigationBar{
    
    self.view.backgroundColor = backgroundcolor;
    naviagtionBar = [[CustomNavigationBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, [self navBarBottom])];
    [self.view addSubview:naviagtionBar];
    naviagtionBar.titleLabel.text = @"选择订房人";
    naviagtionBar.rightBtn.hidden = YES;
    naviagtionBar.rightBtnTitle.hidden = YES;
    [self.view addSubview:naviagtionBar];
    
    [naviagtionBar.leftBtn setImage:[UIImage imageNamed:@"左箭头"] forState:UIControlStateNormal];
    [naviagtionBar.leftBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)AddButtonClick{
    if (self.AddInversepassvalueDelegate && [self.AddInversepassvalueDelegate respondsToSelector:(@selector(Reservationsvalue:PhotoStr:UidStrValue:))]) {
        [self.AddInversepassvalueDelegate Reservationsvalue:self.NameStr PhotoStr:self.PhotoStr UidStrValue:self.uidStr];
    }
    
//    NSDictionary * params = @{@"uid":self.uidStr,@"roomId":self.roomid};
//    WYCLog(@"=============%@",self.uidStr);
//    WYCLog(@"=============%@",self.roomid);
//    [HTTPRequest POST:MSsaveRoomBindingPrincess params:params succeed:^(id responseObject) {
//        WYCLog(@"=============%@",responseObject);
//        if (CodeStr) {
//            [MBManager showBriefAlert:responseObject[@"msg"]];
            //            [self.tableView reloadData];
            [self.navigationController popViewControllerAnimated:NO];
//        }else{
//            
//            [MBManager showBriefAlert:responseObject[@"msg"]];
//        }
//        
//    } failure:^(NSError *error) {
//        [MBManager showBriefAlert:Failed];
//    }];
}
@end
